import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.select_table模块已被废弃，替代模块为qturboframe.public_ui.h_ui.select_table", DeprecationWarning)

from time import sleep

from qturboframe.lib.report.log import logger
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.input_tag import InputTag
from qturboframe.lib.ranorex.web_element.ul_tag import UlTag
from qturboframe.lib.ranorex.web_element.li_tag import LiTag
from qturboframe.lib.ranorex.web_element.span_tag import SpanTag
from qturboframe.lib.ranorex.web_element.i_tag import ITag
from ..ucf_base_element import BaseElement


class SelectTable(BaseElement):
    '''hui的下拉选择器操作'''

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        class_list = self._element.get_attribute('class').split(' ')
        if 'h-selectTable' not in class_list:
            self._element = self._element.find_element_by_attribute(
                'xpath', './ancestor::div[@class~\'h-selectTable\']', timeout=self.timeout)
        self._element = DivTag(self._element.control)

    def set_value_for_single(self, value, timeout=20000):
        """
        hui单选下拉选择器框选择

        :Args:
         - value: 选择项的文本
         - timeout: 超时时间，默认是20s

        :Example:
         - 例1: select.set_value_for_single('苹果')
         - 例2: select.set_value_for_single('苹果',timeout=5000)
        """
        select_div_class = r'h-selectTable-dropdown-list'
        DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-selectTable-selection\']').control).secure_click()
        input_element = self._element.exist(
            'xpath', './/div[@class~\'h-selectTable-selection\']/input', timeout=100)
        if input_element:
            InputTag(input_element.control).send_keys_with_noclick(value)
            # InputTag(input_element.control).focus()
            # from qturboframe.lib.utils.windows.win_desktop import key_input
            # key_input(value)
        dropdown_div = self._element.exist(
            'xpath', './/div[@class~\'h-select-dropdown\']', timeout=100)
        if not dropdown_div:
            dropdown_div = self._driver.find_element_by_attribute(
                'xpath', './body/div[@class~\'h-select-dropdown\']', 'style:display: block;',
                timeout=self.timeout)
        dropdown_div = DivTag(dropdown_div.control)
        timeout1 = 0
        while timeout1 < timeout * 10:
            select_div = dropdown_div.exist(
                'xpath', './/div[@class~\'{}\']'.format(select_div_class), timeout=100)
            if select_div:
                select_div = DivTag(select_div.control)
            if select_div.is_displayed():
                break
            timeout1 += 1000
            sleep(0.1)
        else:
            raise CsClientError('下拉框中没有数据！')
        div = DivTag(select_div.find_element_by_attribute('xpath', './div', timeout=1000).control)
        div_class = div.get_attribute('class').split(' ')
        if 'h-select-block-drop' in div_class:
            select_ul = UlTag(div.find_element_by_attribute(
                'xpath', './/ul[@class~\'h-select-blockblock-content\']', timeout=1000).control)
            list_div = div.exist('xpath','.//div[@class~\'h-select-block-item-header\']', timeout=100)
            if list_div:
                # 下拉多列列表
                pass
            else:
                LiTag(select_ul.find_element_by_attribute(
                    'xpath', './/li[@innertext=\'{}\']'.format(value), timeout=self.timeout).control).secure_click()
        elif 'h-table-wrapper' in div_class:
            # 下拉表格
            pass

    def clear_for_single(self):
        """
        hui单选下拉框清除选项值

        :Example:
         - 例: select.clear_for_single()
        """
        self._element.move_to()
        ITag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-selectTable-selection\']/i[@class~\'h-icon-close\']',
            timeout=self.timeout).control).secure_click()

    def is_default_for_single(self, value):
        """
        检查hui单选下拉框默认值

        :Args:
         - value: 待检查的选项值

        :Returns:
            匹配返回True，否则返回False

        :Example:
         - 例: select.is_default_for_single('香蕉')
        """
        span_value = SpanTag(self._element.find_element_by_attribute('xpath',
            './/div[@class~\'h-selectTable-selection\']/span[@class~\'h-selectTable-selected-value\']',
                                                                     timeout=1000).control)
        text = span_value.text
        if text:
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        span_default = self._element.exist('xpath',
            './/div[@class~\'h-selectTable-selection\']/span[@class~\'h-selectTable-placeholder\']',
            timeout=1000)
        if span_default:
            span_default = SpanTag(span_default.control)
            text = span_default.text
            if value == text:
                return True
            else:
                logger.error('默认值不匹配！')
                return False
        else:
            logger.error('没有默认值！')
            return False

    def is_nonnull(self):
        '''
        检查hui下拉框是否有非空属性

        :Returns:
            非空返回True，否则返回False

        :Example:
         - 例: select.is_nonnull()
        '''
        self._element.focus()
        form_item = DivTag(self._element.find_element_by_attribute(
            'xpath', './ancestor::div[@class~\'h-form-item-content\']/'
                     'parent::div[@class~\'h-form-item\']', timeout=self.timeout).control)
        item_class_list = form_item.get_attribute('class').split(' ')
        if 'h-form-item-required' in item_class_list:
            return True
        else:
            return False

    def check_default_for_single(self, value):
        """
        检查hui单选下拉框默认值

        :Args:
         - value: 待检查的选项值

        :Raises:
            SeleniumError: 下拉框的默认值校验不通过

        :Example:
         -例: select.check_default_for_single('香蕉')
        """
        if not self.is_default_for_single(value):
            raise CsClientError('下拉框的默认值校验不通过！')

    def check_nonnull(self):
        '''
        检查hui下拉框是否有非空属性

        :Raises:
            SeleniumError: 下拉框的非空校验不通过

        :Example:
         - 例: select.check_nonnull()
        '''
        if not self.is_nonnull():
            raise CsClientError('下拉框的非空校验不通过！')
